#!/bin/bash
export HOMEDIR=$(dirname $(dirname $(readlink -f "${BASH_SOURCE[0]}")))
source $HOMEDIR/scripts/init-env.sh

$OSEXEC gs_guc reload -N all -I all -c "track_activities = off"

$OSEXEC gsql -c "create database $ptk_global_biz_dbname encoding ='$ptk_global_biz_db_charset';"
$OSEXEC $GSQLEXEC -c "create domain pg_catalog.long as text;"
$OSEXEC $GSQLEXEC -c "create user sys password 'oracle' sysadmin monadmin opradmin poladmin auditadmin;"
$OSEXEC $GSQLEXEC -c "create user system password 'oracle' sysadmin monadmin opradmin poladmin auditadmin;"
$OSEXEC $GSQLEXEC -c "create extension whale;"
$OSEXEC $GSQLEXEC -c "call copy_error_log_create();"
$OSEXEC $GSQLEXEC -c "call copy_summary_create();"
$OSEXEC $GSQLEXEC -c "grant all on table public.gs_copy_summary to public;"
$OSEXEC $GSQLEXEC -c "grant all on table public.pgxc_copy_error_log to public;"

$OSEXEC bash -c "cd $ptk_global_base_dir/app/compat-tools && $GSQLEXEC -f runMe.sql"

$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set behavior_compat_options='end_month_calculate,compat_sort_group_column,select_into_return_null,aformat_regexp_match,bpchar_coerce_compat,convert_string_digit_to_numeric,char_coerce_compat,proc_outparam_override,proc_implicit_for_loop_variable,allow_procedure_compile_check,plstmt_implicit_savepoint,compat_cursor,aformat_null_test,plsql_security_definer,skip_insert_gs_source,truncate_numeric_tail_zero';"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_select_multi_object_into to on;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_oracle_comment to on;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set sql_beta_feature ='a_style_coerce,extract_pushdown_or_clause';"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_plsql_return_hold_cursor to on;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_plsql_ddl_auto_commit to on;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set max_stack_depth='4MB';"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set plsql_compile_check_options='bind_procedure_public_searchpath,compiledependency_ignore_updated';"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set max_recursive_times=5000;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_nestloop=off;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_index_nestloop=off;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_mergejoin=off;"
$OSEXEC $GSQLEXEC -c "alter database $ptk_global_biz_dbname set enable_date_operator_sub_oracle=on;"

if [ $ptk_global_install_oracle_fdw == 1 ]; then
source $HOMEDIR/scripts/install-oracle-fdw.sh
fi

$OSEXEC gs_guc reload -N all -I all -c "track_activities = on"
